package com.zx.eval.api.front;

import com.zx.common.utils.DateDaySub;
import com.zx.common.utils.DateUtils;
import com.zx.common.utils.R;
import com.zx.common.utils.ShiroUtils;
import com.zx.eval.entity.MemberEntity;
import com.zx.eval.service.MemberService;
import com.zx.sys.app.utils.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller("frontLoginApi")
@RestController()
public class LoginApi {

    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private MemberService memberService;
    @Autowired
    private Environment env;

    @GetMapping("/front/login")
    public R list(@RequestParam Map<String, Object> temp) {
        MemberEntity memberEntity = memberService.memberLogin(temp);
        Long memberEntityId = memberEntity.getId();
        String token = jwtUtils.generateToken(memberEntityId);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());
        map.put("userInfo", memberEntity);

        return R.ok(map);
    }

    @GetMapping("/front/member")
    public R member(@RequestParam Long id) throws Exception {
        String supportTel = new String(env.getProperty("support.tel"));
        String supportInfo = new String(env.getProperty("support.info").getBytes("ISO-8859-1"), "UTF-8");
        String supportTelInfo = new String(env.getProperty("company2.tel.info").getBytes("ISO-8859-1"), "UTF-8");

        MemberEntity memberEntity = memberService.getById(id);
        String format = DateUtils.format(new Date(), DateUtils.DATE_PATTERN);
        if ( memberEntity!= null) {
            String remainDateToString = DateDaySub.remainDateToString(memberEntity.getBirthday(), format);
            memberEntity.setYuefen(remainDateToString);
        }

        return R.ok().put("info", memberEntity).put("supportInfo",supportInfo).put("supportTel",supportTel).put("supportTelInfo",supportTelInfo);
    }

    @RequestMapping("/front/memberSave")
    public R saveMember(MemberEntity memberEntity){
        memberService.save(memberEntity);
        return R.ok();
    }
}
